﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using GomMinhLongBL;
using GomMinhLongDO;
namespace GomMinhLong
{
    public partial class GoiHang : System.Web.UI.Page
    {
        GomMinhLong.ShoppingCart vpp = new GomMinhLong.ShoppingCart();
        private static DataTable tb = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadGioHang();
                load();
            }
        }
        protected void loadGioHang()
        {
            Page.Title = "Giỏ Hàng" + " - " + Ultility.PageTitle();

            HtmlMeta meta = new HtmlMeta();
            meta.Name = "Description";
            meta.Content = Page.Title;
            Page.Header.Controls.Add(meta);

            if (Session["GioHang"] != null)
            {
                tb = (DataTable)Session["GioHang"];
                DataTable dt = new DataTable();
                dt.Columns.Add("STT");
                dt.Columns.Add("SanPhamID");
                dt.Columns.Add("TenSanPham");
                dt.Columns.Add("HinhAnh");
                dt.Columns.Add("SoLuong");
                dt.Columns.Add("DonGia");
                dt.Columns.Add("ThanhTien", typeof(Decimal));

                int i = 0;
                foreach (DataRow dr in tb.Rows)
                {
                    cmsSanPhamDO obj = new cmsSanPhamBL().Select(new cmsSanPhamDO { SanPhamID = Int64.Parse(dr[1].ToString()) });
                    i++;
                    DataRow row = dt.NewRow();
                    row[0] = dr[0];
                    row[1] = dr[1];
                    row[3] = obj.HinhAnh;
                    row[4] = dr[2];
                    row[2] = obj.TenSanPham;
                    row[5] = obj.DonGia;
                    float price = obj.DonGia;
                    row[6] = price * int.Parse(dr[2].ToString());
                    dt.Rows.Add(row);
                }

                if (dt.Rows.Count > 0)
                {
                    GridGioHang.DataSource = dt;
                    GridGioHang.DataBind();
                    float sum = 0;
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        Label lb = GridGioHang.Rows[j].FindControl("lbDG") as Label;
                        int s = int.Parse(dt.Rows[j].ItemArray[5].ToString());
                        lb.Text = s.ToString("##,## VNÐ");
                        lb = GridGioHang.Rows[j].FindControl("lbThanhTien") as Label;
                        string tepm = dt.Rows[j].ItemArray[6].ToString();
                        s = int.Parse(dt.Rows[j].ItemArray[6].ToString());
                        lb.Text = s.ToString("##,## VNÐ");
                        sum += float.Parse(dt.Rows[j].ItemArray[6].ToString());
                    }
                    lblSum.Text = "Tổng: <span style='color:red;font-weight:bold;'>" + sum.ToString("##,## VNÐ") + "</span>";
                    lbThanhTien.Text = "Tổng: <span style='color:green;font-weight:bold;'>" + NumberToStringVN.DocTienBangChu((decimal)sum, " đồng") + "</span";
                }
            }
            else
            {
                lbl.Text = "Chưa có sản phẩm nào trong giỏ hàng!!";
                lblSum.Text = "";
                lbThanhtoan.Visible = false;
            }
        }
        public void Calc(object sender, EventArgs e)
        {
            TextBox tb1 = sender as TextBox;
            GridViewRow row = (GridViewRow)tb1.NamingContainer;
            tb1 = row.FindControl("txtSL") as TextBox;
            Label DG = (Label)row.FindControl("lbDG");
            decimal sum = 0;

            int sl = int.Parse(tb1.Text);
            decimal dg = 0;

            Label TT = (Label)row.FindControl("lbThanhTien");
            decimal ThanhTien = 0;

            dg = Decimal.Parse(DG.Text.Replace(" VNÐ", "").Replace(".", "").Trim());
            ThanhTien = (decimal)sl * dg;

            TT.Text = (sl * dg).ToString("##,## VNÐ");

            for (int i = 0; i < GridGioHang.Rows.Count; i++)
            {
                Label TT1 = (Label)GridGioHang.Rows[i].FindControl("lbThanhTien");
                Decimal t = Decimal.Parse(TT1.Text.Replace(" VNÐ", "").Replace(".", "").Trim());
                sum += t;
            }

            DG.Text = dg.ToString("##,## VNÐ");
            lblSum.Text = "Tổng: <span style='color:red;font-weight:bold;'>" + sum.ToString("##,## VNÐ") + "</span>";
            lbThanhTien.Text = "Bằng chữ: <span style='color:green;font-weight:bold;'>" + NumberToStringVN.DocTienBangChu((decimal)sum, " đồng") + "</span";

            tb = vpp.upDateSL(tb, Int32.Parse(((Label)row.FindControl("lbSTT")).Text), sl, ThanhTien);
        }
        public void load()
        {
            Page.Title = "Giỏ Hàng - " + Ultility.PageTitle();

            HtmlMeta meta = new HtmlMeta();
            meta.Name = "Description";
            meta.Content = Page.Title;
            Page.Header.Controls.Add(meta);
        }
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }
        public int CurrentPage
        {
            get
            {
                // look for current page in ViewState
                object o = this.ViewState["_CurrentPage"];
                if (o == null)
                    return 0;	// default to showing the first page
                else
                    return (int)o;
            }

            set
            {
                this.ViewState["_CurrentPage"] = value;
            }
        }
        protected void GridGioHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridGioHang.PageIndex = e.NewPageIndex;
            loadGioHang();
        }
        protected void GridGioHang_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Int64 id = Int64.Parse(e.CommandArgument.ToString());
            if (e.CommandName == "xoa")
            {
                vpp.delete(tb, id);
                Response.Redirect("/Gio-Hang.htm");
            }
        }
        protected void GridGioHang_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label DonGia = e.Row.FindControl("lbDG") as Label;
                Label ThanhTien = e.Row.FindControl("lbThanhTien") as Label;
                DonGia.Text = string.Format(DonGia.Text, "{0:0,0 VNĐ}");
                ThanhTien.Text = string.Format(ThanhTien.Text, "{0:0,0 VNĐ}");

            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("style", "background-color: #FFFFFF; color: black;");
                e.Row.Attributes.Add("onmouseover", "style.backgroundColor='#CBECF6'");
                e.Row.Attributes.Add("onmouseout", "style.backgroundColor='#FFFFFF'");
            }
        }
    }
}